import { getAllChannels } from '@/api/channel'
const state = {
  UserChannel: {},
  allChannel: {}
}
const mutations = {
  setUserChannel(state, value) {
    state.UserChannel = value
  },
  setAllChannel(state, value) {
    state.allChannel = value
  },
  adduserchannel(state, item) {
    state.UserChannel.push(item)
  },
  deluserchannel(state, id) {
    state.UserChannel = state.UserChannel.filter(item => {
      return item.id !== id
    })
  }
}

const actions = {
  async getAllChannel(context) {
    const res = await getAllChannels()
    context.commit('setAllChannel', res.data.channels)
  }
}
const getters = {
  optionChannel(state) {
    return state.allChannel.filter(item => {
      const str = state.UserChannel.some(channel => {
        return channel.id === item.id
      })
      if (str) {
        return false
      } else {
        return true
      }
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
